package com.cutthroatcreations.woodenrailway;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.cutthroatcreations.woodenrailway.SectionedAdapter.SectionedListAdapter;
import com.cutthroatcreations.woodenrailway.SectionedAdapter.SectionedListItem;
import com.cutthroatcreations.woodenrailway.SectionedAdapter.SectionedListView;
import com.cutthroatcreations.woodenrailway.json.JSONObject;
import com.google.gson.Gson;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CollectionManagerActivity extends Activity {

	private static final String TAG = "CollectionManager";

	private StandardArrayAdapter arrayAdapter;

	private SectionedListAdapter sectionAdapter;

	private SectionedListView listView;

	private ContentWorker cw;

	private String response;

	private class StandardArrayAdapter extends ArrayAdapter<SectionedListItem> {

		private final SectionedListItem[] items;
		private final Context ctx;

		public StandardArrayAdapter(final Context context,
				final int textViewResourceId, final SectionedListItem[] items) {
			super(context, textViewResourceId, items);
			this.ctx = context;
			this.items = items;
		}

		@Override
		public View getView(final int position, final View convertView,
				final ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.collection_manager_list_view, null);
			}
			final SectionedListItem currentItem = items[position];
			if (currentItem != null) {
				final TextView textView = (TextView) view
						.findViewById(R.id.item_text_view);
				final ImageView imageMinus = (ImageView) view
						.findViewById(R.id.listColMan);

				imageMinus.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String y = currentItem.section;
						if (y == "1992/1993")
							y = "1993";
						ContentWorker cw = new ContentWorker(ctx,
								currentItem.item, y);
						int result = cw.checked();
						Log.i(TAG, String.valueOf(result));
						if (result > 0) {
							Log.i(TAG, String.valueOf(result));
							imageMinus.setImageBitmap(null);
							textView.setBackgroundColor(Color
									.parseColor("#FF0000"));

						}

					}
				});
				if (textView != null) {
					textView.setText(currentItem.item.toString());
				}
			}
			return view;
		}
	}

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.collection_main);
		cw = new ContentWorker(this);
		arrayAdapter = new StandardArrayAdapter(this, R.id.item_text_view,
				cw.getCollectionAsItems());
		sectionAdapter = new SectionedListAdapter(getLayoutInflater(),
				arrayAdapter);
		listView = (SectionedListView) findViewById(R.id.section_list_view);
		listView.setAdapter(sectionAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.col_list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.export_collection:

			ArrayList<Owned> owned = cw.getCollectionForExport();

			Gson gson = new Gson();
			String jStr = gson.toJson(owned);

			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("items", jStr));
			postParameters.add(new BasicNameValuePair("user_id", String.valueOf(WoodenApplication.user.getId())));
			postParameters.add(new BasicNameValuePair("app", "1"));

			try {
				response = CustomHttpClient.executeHttpPost("users/export",
						postParameters);
			} catch (Exception e) {
				e.printStackTrace();
				response = null;
			}

			return response.equals("1");

		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
